package sealte.util.xml;

import java.io.FileInputStream;
import java.io.FileReader;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import sealte.util.file.FileUtil;

public class XMLReader {
	
	private XMLReader(){}
	
	public static String getXMLEncoding(String filePath) throws Exception {
		FileReader reader = null;
		String encoding = null;
		try{
			reader = new FileReader(filePath);
			encoding = reader.getEncoding();
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}finally{
			if(null!=reader){
				reader.close();
			}
		}
		return encoding;
	}
	
	public static String getHeaderEncoding(String filePath) throws Exception {
		String encoding = "UTF-8";
		String content = FileUtil.readFile(filePath).toLowerCase();
		if(content.indexOf("encoding")>-1){
			String[] cons1 = content.split("encoding=\"");
			String[] cons2 = cons1[1].split("\"");
			encoding = cons2[0];
		}
		return encoding;
	}
	
	public static String getElementText(String filePath,String[] path) throws Exception {
		return getElement(filePath,path).getText();
	}
	
	public static Attribute getAttribute(String filePath,String[] path,String attribute) throws Exception {
		return getElement(filePath,path).getAttribute(attribute);
	}
	
	public static String getAttributeValue(String filePath,String[] path,String attribute) throws Exception {
		return getAttribute(filePath,path,attribute).getValue();
	}
	
	public static List<Element> getElements(String filePath,String[] path) throws Exception {
		return getElement(filePath,path).getChildren();
	}
	
	public static Element getElement(String filePath,String[] path) throws Exception {
		Document doc = getDocument(filePath);
		Element root = doc.getRootElement();
		return getElement(root,path);
	}
	
	public static Document getDocument(String filePath) throws Exception {
		SAXBuilder sab = new SAXBuilder();
		return sab.build(new FileInputStream(filePath));
	}
	
	public static Element getElement(Element root,String[] path) throws Exception {
		Element ele = null;
		if(null==path){ path = new String[]{};}
		switch(path.length){
		case 0:
			ele = root;
			break;
		case 1:
			ele = root.getChild(path[0]);
			break;
		case 2:
			ele = root.getChild(path[0]).getChild(path[1]);
			break;
		case 3:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]);
		    break;
		case 4:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]).getChild(path[3]);
			break;
		case 5:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]).getChild(path[3])
			          .getChild(path[4]);
			break;
		case 6:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]).getChild(path[3])
			          .getChild(path[4]).getChild(path[5]);
			break;
		case 7:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]).getChild(path[3])
	                  .getChild(path[4]).getChild(path[5]).getChild(path[6]);
			break;
		case 8:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]).getChild(path[3])
                      .getChild(path[4]).getChild(path[5]).getChild(path[6]).getChild(path[7]);
			break;
		case 9:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]).getChild(path[3])
            		  .getChild(path[4]).getChild(path[5]).getChild(path[6]).getChild(path[7])
            		  .getChild(path[8]);
			break;
		case 10:
			ele = root.getChild(path[0]).getChild(path[1]).getChild(path[2]).getChild(path[3])
  		  			  .getChild(path[4]).getChild(path[5]).getChild(path[6]).getChild(path[7])
  		  			  .getChild(path[8]).getChild(path[9]);
			break;
		}
		return ele;
	}
	
}
